package com.qianfeng.smsplatform.gateway.filters;

//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼                  BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  
//  


import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.qianfeng.smsplatform.common.constants.CacheConstants;
import com.qianfeng.smsplatform.gateway.api.CacheService;
import com.qianfeng.smsplatform.gateway.cache.ApiMappingFilterCache;
import com.qianfeng.smsplatform.gateway.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by Jackiechan on 2022/2/14/10:16
 *
 * @author Jackiechan
 * @version 1.0
 * @since 1.0
 */

@Component
public class SignFiler extends ZuulFilter {
    private CacheService cacheService;
    private ApiMappingFilterCache apiMappingFilterCache;


    @Autowired
    public void setApiMappingFilterCache(ApiMappingFilterCache apiMappingFilterCache) {
        this.apiMappingFilterCache = apiMappingFilterCache;
    }

    @Autowired
    public void setCacheService(CacheService cacheService) {
        this.cacheService = cacheService;
    }

    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return FilterConstants.PRE_DECORATION_FILTER_ORDER+100+apiMappingFilterCache.getIndex(this.getClass().getSimpleName().toLowerCase());
    }

    @Override
    public boolean shouldFilter() {
        boolean shouldFilter = apiMappingFilterCache.shouldFilter(this.getClass().getSimpleName().toLowerCase());
        RequestContext currentContext = RequestContext.getCurrentContext();
        boolean zuulResponse = currentContext.sendZuulResponse();//根据前置过滤器的结果来决定是否启用当前过滤器,前面拦截这个就不执行
       // return zuulResponse;
        return zuulResponse&&shouldFilter;
    }

    @Override
    public Object run() throws ZuulException {
        //获取到传递的参数
        RequestContext context = RequestContext.getCurrentContext();
        HttpServletRequest httpServletRequest = context.getRequest();
        Map<String, String[]> parameterMap = httpServletRequest.getParameterMap();
        TreeMap<Object, Object> treeMap = new TreeMap<>();
        treeMap.putAll(parameterMap);//用排序的map保存数据,这样参数就自然而然排序了
        //获取到当前客户的密码
        String secure = cacheService.hGet(CacheConstants.CACHE_PREFIX_CLIENT + httpServletRequest.getParameter("clientId"), "secure");
        //按照前面规则生成签名
        //和传递的签名进行比较
        boolean checkSign = MD5Util.checkSign("UTF-8", treeMap, secure);
        if (!checkSign) {
            //检查失败,签名不对
            context.setSendZuulResponse(false);
            context.addZuulResponseHeader("content-Type","text/html;charset=utf-8");
            context.setResponseBody("签名校验失败");
        }

        return null;
    }
}
